package cm.util.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import cm.util.dao.GenericDao;
import cm.util.dao.GenericDaoHibernateImpl;
import cm.util.exception.MessageException;
import cm.util.pojo.BasicPojo;

public class GenericServiceImpl<E> implements GenericService<E> {

	private static final Log log = LogFactory.getLog(GenericServiceImpl.class);

	protected GenericDao<E> dao;

	protected Session session;

	/**
	 * 根据实体类类对象构造Service
	 * 
	 * @param entityClass
	 */
	public GenericServiceImpl(Class<E> entityClass) {
		dao = new GenericDaoHibernateImpl<E>(entityClass);
		session = dao.getSession();
	}

	/**
	 * 根据实体类名称构造Service
	 * 
	 * @param entityClass
	 */
	public GenericServiceImpl(String entityClassName) {
		dao = new GenericDaoHibernateImpl<E>(entityClassName);
		session = dao.getSession();
	}

	/**
	 * 根据Dao对象名称构造Service
	 * 
	 * @param entityClass
	 */
	public GenericServiceImpl(GenericDao<E> dao) {
		this.dao = dao;
		session = dao.getSession();
	}
	
	/**
	 * 添加实体对象
	 * 如果实体对象继承于BasicPojo则判断其ID是否已存在再添加，
	 * 否则直接添加
	 */
	public String add(E entity) {
		String id;
		session = dao.getSession();
		
		
		if(entity instanceof BasicPojo){
			BasicPojo basicPojo=(BasicPojo)entity;
			Transaction tran = session.beginTransaction();
			if(basicPojo.getId()!=null){
				E old=dao.getForUpdate(basicPojo.getId().toString());
				if(old!=null){
					tran.commit();
					throw new MessageException("已存在代号为："+basicPojo.getId()+"的对象！");
				}
			}
			id = dao.save(entity);
			tran.commit();
			session.flush();
			session.clear();
			return id;
		}
		Transaction tran = session.beginTransaction();
		id = dao.save(entity);
		tran.commit();
		session.flush();
		session.clear();
		session.close();
		return id;
	}

	public void delete(List<String> ids) {
		session = dao.getSession();
		Transaction tran = session.beginTransaction();
		try{
			for (String id : ids) {
				dao.delete(id);
			}
			tran.commit();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			session.flush();
			session.clear();
			session.close();
		}
		
		
		
	}

	public void delete(String id) {
		session = dao.getSession();
		Transaction tran = session.beginTransaction();
		dao.delete(id);
		tran.commit();
		session.flush();
		session.clear();
		session.close();
	}

	public E detail(String id) {
		E e=dao.get(id);
		session.close();
		return e;
	}

	public List<E> findAll(int currentPage, int pageSize) {
		List<E> list=dao.findAll(pageSize * (currentPage - 1), pageSize);
		session.flush();
		session.clear();
		session.close();
		return list;
	}

	public long sizeAll() {

		return dao.countAll();
	}

	public void update(E entity) {
		session = dao.getSession();
		Transaction tran = session.beginTransaction();
		dao.update(entity);
		tran.commit();
		session.flush();
		session.clear();
		session.close();
	}

	public void batchUpdate(Map<String, ?> equalMap, Map<String, ?> updateMap) {
		session = dao.getSession();
		Transaction tran = session.beginTransaction();
		dao.batchUpdate(equalMap, updateMap);
		tran.commit();
		session.flush();
		session.clear();
		session.close();
	}
}
